package com.lanren.familybookapp.ui.me.presenter;

import com.lanren.common.base.impl.BasePresenter;
import com.lanren.familybookapp.ui.me.bean.BookInfoBean;
import com.lanren.familybookapp.ui.me.interfaces.IManagerModel;
import com.lanren.familybookapp.ui.me.model.BookManagerModel;

import java.util.List;

/**
 * @ClassName ManagerPresenter
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/10/11 19:11
 * @Version 1.0
 */
public class ManagerPresenter extends BasePresenter<IManagerModel.IManagerView> {

    private BookManagerModel bookManagerModel;

    public ManagerPresenter(BookManagerModel bookManagerModel) {
        this.bookManagerModel = bookManagerModel;
    }

    public void getBookList(int page) {
        bookManagerModel.getBookList(page, new IManagerModel.ManagerModel() {

            @Override
            public void onError(int code, String msg) {
                mRootView.showError(code, msg);
            }

            @Override
            public void onSuccess(List<BookInfoBean> bookInfoBeans) {
                mRootView.onSuccess(bookInfoBeans);
            }

        });
    }

    public void addBook(String bookName) {
        bookManagerModel.addBook(bookName, new IManagerModel.ManagerBookAddModel() {
            @Override
            public void onError(int code, String msg) {
                mRootView.showError(code, msg);
            }

            @Override
            public void onSuccess(String msg) {
                mRootView.addBookSuccess(msg);
            }
        });
    }

    public void delBook(int bookId) {
        bookManagerModel.delBook(bookId, new IManagerModel.ManagerBookAddModel() {
            @Override
            public void onError(int code, String msg) {
                mRootView.showError(code, msg);
            }

            @Override
            public void onSuccess(String msg) {
                mRootView.addBookSuccess(msg);
            }

        });
    }

    public void updateUseBook(int userUseBook) {
        bookManagerModel.updateUseBook(userUseBook, new IManagerModel.ManagerBookAddModel() {
            @Override
            public void onError(int code, String msg) {
                mRootView.showError(code, msg);
            }

            @Override
            public void onSuccess(String msg) {
                mRootView.useBookSuccess(msg);
            }

        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (bookManagerModel != null) {
            bookManagerModel.clearDisposable();
            bookManagerModel = null;
        }
    }
}
